package Base64;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * Created with IntelliJ IDEA.
 * 类名：MainTest
 * 开发人员: CoderJu
 * 创建时间: 2019/3/9 23:43
 * 描述:
 * 版本：V1.0
 */
public class MainTest {
    public static void main(String[] args) throws UnsupportedEncodingException {
        //BaseMethod();
        //URLMethod();
        MIMEmethod();
    }

    private static void MIMEmethod() throws UnsupportedEncodingException {
        //编码
        String str = "qwert123d3r3f3c3hgfdszxcvbnm";
        System.out.println("base64 MIME 编码 >>>>> " +Base64.getUrlEncoder().encodeToString(str.getBytes("utf-8")));
        //cXdlcnQxMjNkM3IzZjNjM2hnZmRzenhjdmJubQ==
        //解码
        System.out.println("base64 MIME 解码 >>>>> " + new String(Base64.getUrlDecoder().decode("cXdlcnQxMjNkM3IzZjNjM2hnZmRzenhjdmJubQ==".getBytes("utf-8"))));
    }

    private static void URLMethod() throws UnsupportedEncodingException {
        //编码
        String str = "qwert123d3r3f3c3hgfdszxcvbnm";
        System.out.println("base64 URL 编码 >>>>> " +Base64.getUrlEncoder().encodeToString(str.getBytes("utf-8")));
        //解码
        System.out.println("base64 URL 解码 >>>>> " + new String(Base64.getUrlDecoder().decode("cXdlcnQxMjNkM3IzZjNjM2hnZmRzenhjdmJubQ".getBytes("utf-8"))));
    }

    public static void BaseMethod() throws UnsupportedEncodingException {
        //编码
        String str = "我是JAVA";
        System.out.println("base64编码 >>>>> "+Base64.getEncoder().encodeToString(str.getBytes("utf-8")));
        //解码 5oiR5pivSkFWQQ==
        System.out.println("base64解码 >>>>>>> "+new String (Base64.getDecoder().decode("5oiR5pivSkFWQQ==".getBytes("utf-8"))));

    }

}
